<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/22
 * Time: 17:15
 */

namespace app\wap\controller;


use think\Db;
use app\api\controller\KbxOrder as KbxOrder;
use app\admin\controller\Membertype;
use think\log;

class Member extends Basewap
{
    public  $statusArr = array(
        '1' => array(
            'orderStatus' => 'pending',
            'paymentStatus' => 'pending',
        ),
        '2' => array(
            'orderStatus' => 'toBeStarting',
            'paymentStatus' => 'paid',
        ),
        '3' => array(
            'orderStatus' => 'finished',
            'paymentStatus' => 'paid',
        ),
        '4' => array(
            'orderStatus' => 'reviewed',
            'paymentStatus' => 'paid',
        ),
        '5' => array(
            'orderStatus' => 'toBeStarting',
            'paymentStatus' => 'refunding',
        ),
        '6' => array(
            'orderStatus' => 'canceled',
            'paymentStatus' => 'refunding',
        ),
        '7' => array(
            'orderStatus' => 'expired',
            'paymentStatus' => 'pending',
        ),
        '8' => array(
            'orderStatus' => 'canceled',
            'paymentStatus' => 'refunded',
        ),
        '9' => array(
            'orderStatus' => 'toBeStarting',
            'paymentStatus' => 'rejected',
        ),
        '10' => array(
            'orderStatus' => 'unknown',
            'paymentStatus' => 'pending',
        )
    );

    //收藏
    public function collection(){
        $member_id= input('member_id');
        $member=db('member')->where('member_id',$member_id)->find();
        $collection1 = model('Collection')
            ->where('member_id',$member_id)
            ->order('creattime desc')
            ->select();
        $data=[];
        $img1='';

        $Membertype = new Membertype();
        $discoun_per_arr = $Membertype->get_discoun_per($member['member_id']);//获取折扣集

        foreach ($collection1 as $k=>$v){
            if($v['collection_types']==1){
                $goods=  db('goods')->where('goods_id',$v['goods_id'])
                    ->field('goods_id,goods_name,goods_englishname,goods_image,show,price,classify_id')
                    ->select();
                $price='';
                //该类型商品的折扣率
                foreach ($goods as $l=>$s){
                    $discoun_per = isset($discoun_per_arr[$s['classify_id']]) ? $discoun_per_arr[$s['classify_id']] : 1;
                    $specs1=model('Specs')->alias('s')
                        ->where('s.goods_id',$s['goods_id'])
                        ->where('s.specs_status','1')
                        ->join('Person p','s.specs_id=p.specs_id')
                        ->where('p.person_status','1')
                        ->field('p.person_price')
                        ->select();

                    if (!empty($specs1)){
                        foreach ($specs1 as $k=>$v){
                            $price[]=$v['person_price'];
                        }
                        $max1= max($price);
                        $min1= min($price);
                        if ($max1==$min1){
                            $s['qujian1']=ceil(($min1*$discoun_per));
                        }else{
                            $s['qujian1']=  ceil(($min1*$discoun_per)).'~'.ceil(($max1*$discoun_per));
                        }
                    }
                    $img= !empty($s['goods_image'])?explode(',',$s['goods_image']):'';
                    if(!empty($img['1'])){
                        $s['goods_image'] = $img['1'];
                        $s['img_type']=1;
                    }
                    if($s['show']==2){
                        $exchange= db('exchange')->where('currency_code',"USD")->find();//汇率
                        $s['qujian1']=ceil($s['price']*$exchange['exchange_rate']);
                        if (!empty($discoun_per)){
                            $s['qujian1']= ceil($s['qujian1']*$discoun_per);
                        }
                    }
                    $data['goods'][]=$s;
                }
            }
        }

        return json_encode(['msg'=>'','code'=>0,'data'=>$data]);
    }

    /**
     * 我的优惠卷
     */
    public function coupon(){
        $member_id=input('member_id');
        if (empty($member_id)) return json_encode(['msg'=>'用户错误','code'=>101,'data'=>'']);
        $d= db('coupon')
            ->alias('a')
            ->join('coupondetails d','a.coupon_id=d.coupon_id')
            ->where('d.member_id',$member_id)
            ->field('a.*,d.id as lid,d.starttime as lingqushijian,d.usestatus')
            ->select();
        $data=[];
        $shu=[];

        if (!empty($d)) {
            foreach ($d as $v) {
                if ($v['usestatus'] == 0) {
                    if ($v['end_usetime'] < time() && $v['time_use_type'] == 2) {
                        continue;
                    } else {
                        $v['full_reduction'] = '';
                        if ($v['coupon_types'] == 2) {
                            $v['full_reduction'] = $v['coupon_discount'];
                            $v['coupon_discount'] = '';
                        }
                        if($v['time_use_type'] == 2){
                            $validityperiod = date('Y.m.d', $v['etart_usetime']) . ' - ' . date('Y.m.d', $v['end_usetime']);
                        }elseif ($v['time_use_type'] == 3){
                            $t=$v['lingqushijian']+(86400*$v['end_useday']);
                            if ($t<time() && $v['time_use_type'] == 3){
                                db('coupondetails')->where('id',$v['lid'])->update([
                                    'usestatus'=>2
                                ]);
                                continue;
                            }

                            $validityperiod = date('Y.m.d', $v['lingqushijian']) . ' - ' . date('Y.m.d', $t);
                        }else{
                            $validityperiod ='无期限';
                        }
                        $data[] = [
                            "coupon_id" => $v['coupon_id'],
                            "coupon_name" => $v['coupon_name'],
                            "discount" => $v['coupon_discount'],//折扣
                            "full_reduction" => $v['full_reduction'],//满减
                            "validityperiod" =>$validityperiod ,//使用条件
                            "coupon_usecondition" => $v['coupon_usecondition'],//使用条件
                            "usestatus" => $v['usestatus'],//
                            "coupon_type" =>'OWN',//
                        ];
                    }
                }
            }
        }
        $Kbxcoupon =new Kbxcoupon();
        $da=$Kbxcoupon->usercoupon($member_id);
        if (!empty($da)){
            $da=json_decode($da,true);
            foreach ($da as $i){
                if ($i['locked']==false  && $i['amount']!=0){
                    $data[] = [
                        "coupon_id" => $i['_id'],
                        "coupon_name" => $i['coupon_name'],
                        "discount" => '',//折扣
                        "full_reduction" => $i['amount'],//满减
                        "validityperiod" =>'无限期',//使用条件
                        "coupon_usecondition" => !empty($i['restrictions']['minPrice'])?$i['restrictions']['minPrice']:0,//使用条件
                        "usestatus" => '',//
                        "coupon_type" =>'KBX',//
                    ];
                }
            }
        }
        return json_encode(['msg'=>'','code'=>0,'data'=>$data]);
    }

    //积分日志
    public function integralLog(){
        $member_id= input('member_id');
        $data = db('member_integrallog')->where(array('member_id'=>$member_id))->order('create_time desc')->select();
        $kk=[];
        foreach ($data as $v){
            $v['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $kk[]=$v;
        }
        return json_encode(['msg'=>'','code'=>0,'data'=>$kk]);
    }

    /*
     * 我的订单
     * @param string $status    订单状态:1=待付款,2=已付款待使用,3=已使用待评价,4=已评价,5=退款中,6=已取消
     */
    public function order(){
        error_reporting(E_ERROR);

        $status= input('status');
        $member_id= input('member_id');
        $status= !empty($status)?$status:'';
        $where['member_id'] = $member_id;
        $data=[];
        if($status!=''){
            $where['order_status'] = $status;
        }
        $data2 = db('order')->where($where)->where('isdelete',0)->order('creattime','desc')->select();
        foreach ($data2 as $k=>$v){
            $goods=db('goods')->where('goods_id',$v['goods_id'])->find();
            $v['orderType'] = $goods['orderType'];
            if (empty($v['orderperson'])){
                $person=db('orderperson')->where('order_id',$v['order_id'])->field('person_name,person_num')->select();
            }else{
                $person=json_decode($v['orderperson'],true);
                unset($v['orderperson']);
            }

            //判断是否可以评论
            if ($v['comment_type'] == 0 && strtotime($v['travel_date'])<time()){
                $v['comment_type'] = '1' ;
            }

            $v['goods_name']=$goods['goods_name'];
            if (!empty($v['creattime'])){
                $creatAt = $v['creattime'];
                $v['creattime']=date('Y-m-d H:i:s',$v['creattime']);
            }
            if (!empty($v['updatetime'])){
                $v['updatetime']=date('Y-m-d H:i:s',$v['updatetime']);
            }
            if ($person==true){
                $v['person']=$person;
            }else{
                return json_encode(['msg'=>'当前没有订单','code'=>0,'data'=>'']);
            }
            $data[$creatAt]=$v;
        }

        //查询订单系统  订单list
        $KbxOrder = new KbxOrder();
        $list = $KbxOrder->kbx_order_list($member_id);
        if (!empty($list)){
            foreach ($list as $k=>$s){
                if($s['order_status']==$status||empty($status)){
                    $data[$k]=$s;
                }
            }
        }
        krsort($data);
        $data = array_values($data);
        $exchangeArr = Db::name("exchange")->select();
        foreach ($data as $key=>$value) {
            $cny_fold_money = $value['fold_money'];
            $moneyArr = [["code"=>"CNY","money"=>$cny_fold_money]];
            if($value['order_status']==1){
                //美元汇率
                if(!empty($exchangeArr)){
                    foreach ($exchangeArr as $k=>$v) {
                        $v_money = $cny_fold_money/$v['exchange_rate'];
                        $v_money = ceil($v_money*100)/100;
                        $moneyArr[] = array("code"=>$v['currency_code'],"money"=>$v_money);
                    }
                }
            }
            $data[$key]['moneyArr'] = $moneyArr;
        }

        return json_encode(['msg'=>'','code'=>0,'data'=>$data]);
    }


    //订单评论
    public function orderevaluate(){
        $order_id= input('order_id');
        $order_no= input('order_no');
        $where['member_id']= input('member_id');
        $data = db('order')->where('order_id',$order_id)->where('isdelete',0)->find();
        if(empty($data)){
            $KbxOrder = new KbxOrder();
            $datalist = $KbxOrder->kbx_order_by_no($order_no);
            if(count($datalist) == 1 || empty($datalist)){
                return json_encode(['msg'=>'当前没有订单','code'=>0,'data'=>$data]);
            }
            $data['original_money']=$datalist['payments']['foldMoney'];
            $data['goods_name']=$datalist['goods']['goodsName'];
            $data['creattime']=date('Y-m-d H:i:s',strtotime($datalist['createdAt']));
            $data['person']=$datalist['itinerary']['orderPerson'];
            $data['order_no']=$order_no;
            $data['order_id']=$datalist['orderId'];
        }else{
            $goods_name=db('goods')->where('goods_id',$data['goods_id'])->field('goods_name')->find();
            if (empty($data['orderperson'])){
                $person=db('orderperson')->where('order_id',$data['order_id'])->field('person_name,person_num')->select();
            }else{
                $person=json_decode($data['orderperson'],true);
                unset($data['orderperson']);
            }

            if (!empty($person)){
                $data['person']=$person;
            }else{
                return json_encode(['msg'=>'当前没有订单','code'=>0,'data'=>'']);

            }

            $data['goods_name']=$goods_name['goods_name'];
            if (!empty($data['creattime'])){
                $data['creattime']=date('Y-m-d H:i:s',$data['creattime']);
            }
            if (!empty($data['updatetime'])){
                $data['updatetime']=date('Y-m-d H:i:s',$data['updatetime']);
            }
        }


        return json_encode(['msg'=>'ok','code'=>0,'data'=>$data]);
    }

    /**
     * 未付款取消订单
     * @param $order_id
     */
    public function removeOrder($order_id){
        $order_id= input('order_id');
        $order = model('order')->where(['isdelete'=>0])->find($order_id);
        if($order['order_status']==1){
            $result = $order->delete();
//            db('orderperson')->where('order_id',$order_id)->update(['isdelete'=>1]);
//            if($result){

            if ($order['coupon_type']=='KBX'){
                if (!empty($order['cost_ticket_id'])){
                    $Kbxcoupon=  new Kbxcoupon();
                    $d= $Kbxcoupon->refundCashCoupon($order['member_id'],$order['cost_ticket_id'],$order['order_no']);
                    if (empty($d)){
                        $d=true;

                    }else{
                        $d=json_decode($d,true);
                        if (!empty($d['msg'])) return json_encode(['msg'=>$d['msg'],'code'=>101]);
                    }
                    if ($d!=true){
                        return json_encode(['msg'=>'删除失败','code'=>101,'data'=>'1']);
                    }
                }
            }else{
                db('coupondetails')->where('id',$order['cost_ticket_id'])->update(['usestatus'=>0]);
            }

            return json_encode(['msg'=>'取消成功','code'=>0,'data'=>'']);

//            }else{
//                return json_encode(['msg'=>'取消失败','code'=>0,'data'=>'']);
//            }
        }else{
            return json_encode(['msg'=>'只有未付款订单才能取消','code'=>101,'data'=>'']);
        }
    }

    /**
     * 订单删除
     * @param $order_id
     */
    public function delorder(){
        $order_id=input('order_id');
        $order_no=input('order_no');
        $orderType=input('orderType');
        $orderclass=input('orderclass');//用于区分是删除订单还是只退回优惠券
        $orderclass=!empty($orderclass)?$orderclass:0;
        $data['order_id'] = $order_id;
        $data['isdelete'] = 1;
        $d='';
        $dat=db('order')->where('order_id',$order_id)->find();

        if(!empty($dat)){
            if ($dat['coupon_type']=='KBX' && $dat['order_status']<3){
                if (!empty($dat['cost_ticket_id'])){
                    $Kbxcoupon = new Kbxcoupon();
                    $d = $Kbxcoupon->refundCashCoupon($dat['member_id'], $dat['cost_ticket_id'], $dat['order_no']);
                    if($orderclass != 1){
                        if (empty($d)) {
                            $d = db('order')->where('order_id',$order_id)->update([
                                'isdelete'=>1,
                            ]);
                            $d = true;
                        } else {
                            $d = json_decode($d, true);
                            if (!empty($d['msg'])) return json_encode(['msg' => $d['msg'], 'code' => 101]);
                        }
                        if ($d != true) {
                            return json_encode(['msg' => '删除失败', 'code' => 101, 'data' => '1']);
                        }
                    }

                }
            }else{
                db('coupondetails')->where('id',$dat['cost_ticket_id'])->update(['usestatus'=>0]);
                if($orderclass != 1){
                    if($dat['order_status']<2){
                        $d = db('order')->where('order_id',$order_id)->update([
                            'isdelete'=>1,
                        ]);

                        if ($d != true) {
                            return json_encode(['msg' => '删除失败', 'code' => 101, 'data' => '1']);
                        }
                    }
                }
            }
        }else{//mongo
            $KbxOrder = new KbxOrder();
            $dat = $KbxOrder->kbx_order_by_no($order_no);
            if (isset($dat['payments']['couponType']) && $dat['payments']['couponType']=='KBX' && $dat['status']['orderStatus']=='pending'){
                if (!empty($dat['payments']['costTicketId'])){
                    $Kbxcoupon = new Kbxcoupon();
                    $d = $Kbxcoupon->refundCashCoupon($dat['booker']['memberId'], $dat['payments']['costTicketId'], $order_no);
                    if (empty($d)) {
                        //订单系统  更新order数据
                        $postUpdateArr = [
                            'id'=>$order_id,
                            'isDelete'=>1,
                        ];
                        $update_res = $KbxOrder->kbx_operate_order($orderType,$postUpdateArr);
                        $log[]=[
                            '删除订单：删除成功，退回优惠券'=>json_encode($postUpdateArr),
                            'res'=>$update_res
                        ];
                        Log::record($log);
                        $d = true;
                    } else {
                        $d = json_decode($d, true);
                        if (!empty($d['msg'])) return json_encode(['msg' => $d['msg'], 'code' => 101]);
                    }
                    if ($d != true) {
                        return json_encode(['msg' => '删除失败', 'code' => 101, 'data' => '1']);
                    }
                }
            }else{
                if(isset($dat['payments']['costTicketId'])){
                    db('coupondetails')->where('id',$dat['payments']['costTicketId'])->update(['usestatus'=>0]);
                }

                if($dat['status']['paymentStatus']!=='paid'){
                    //订单系统  更新order数据
                    $postUpdateArr = [
                        'id'=>$order_id,
                        'isDelete'=>1,
                    ];
                    if($orderType=="CarPool"){
                        $update_res = $KbxOrder->deletePersonalCarPoolOrder($order_id);
                    }elseif($orderType=="CustomMade"){
                        $update_res = $KbxOrder->deletePersonalCustomMadeOrder($order_no);
                    }else{
                        $update_res = $KbxOrder->kbx_operate_order($orderType,$postUpdateArr);
                    }
                    $log[]=[
                        '删除订单：删除成功'=>json_encode($postUpdateArr),
                        'res'=>$update_res
                    ];
                    Log::record($log);
                    $d = true;
                    if ($update_res != true) {
                        return json_encode(['msg' => '删除失败', 'code' => 101, 'data' => '1']);
                    }
                }
            }
        }

        if ($d == true) {
            return json_encode(['msg'=>'删除成功','code'=>0,'data'=>'']);
        }
    }

    /**
     * 评论显示
     * @param $order_id
     */
    public function evaluate($order_id){
        $order_id= input('order_id');
        $order = model('order')->where(['isdelete'=>0])->where('order_id',$order_id)->find();
        $goods = model('Goods')->where('goods_id',$order['goods_id'])->find();
        $data['fold_money'] = $order['fold_money'];
        $data['totalnum'] = $order['totalnum'];
        $data['starttime'] = date('Y-m-d H:i',$goods['starttime']);
        $data['order_id'] = $order['order_id'];
//        $this->success('',$data,0);
        $shu=[
            'code'=>0,
            'msg'=>'ok',
            'data'=>$data

        ];

        return json_encode($shu);
    }

    //新增订单评论
    public function evaluateAdd(){
        $post=request()->post();
        if ( !empty($post)){
            $comment= db('comment')->where([ 'member_id'=>$post['member_id'], 'goods_id'=>$post['goods_id']])->find();
            $creattime= $comment['creattime']+30;
            if (time()<$creattime){
                $shu=[
                    'code'=>101,
                    'msg'=>'30秒内不能重复提交评论',
                ];
                return json_encode($shu);
            }
            Db::startTrans();
            try{
                $integral= db('integral')->where('types',2)->find();
                $result = db('comment')->insert([
                    'content'=>$post['content'],
                    'comment_img'=>$post['content_img'],
                    'member_id'=>$post['member_id'],
                    'goods_id'=>$post['goods_id'],
                    'add_integral'=>$integral['integra'],
                    'creattime'=>time()
                ]);
                $comment_id=Db::getLastInsID();

                db('member_integrallog')->insert([
                    'member_id'=>$post['member_id'],
                    'describe'=>'参与评论',
                    'integral'=>$post['integral'],
                    'create_time'=>time(),
                ]);
                $member_integral= db('member')->where('member_id',$post['member_id'])->field('member_integral')->find();
                $where = (int)$member_integral['member_integral']+(int)$post['integral'];
                db('member')->where('member_id',$post['member_id'])->update([
                    'updatetime'=>time(),
                    'member_integral'=>$where,
                ]);

                db('order')->where('order_id',$post['order_id'])->update([
                    'order_status'=>4,
                    'comment_type'=>2
                ]);

                // 提交事
                Db::commit();
                $shu=[
                    'code'=>0,
                    'msg'=>'评论成功',
                    'cuowu'=>$comment_id
                ];

                return json_encode($shu);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $shu=[
                    'code'=>1,
                    'msg'=>'评论失败',
                    'cuowu'=>$e->getMessage()
                ];

                return json_encode($shu);
            }
        }
    }

    //退款按钮
    public function refund(){
        $order_id=input('order_id');
        $order_no=input('order_no');
        $member_id=input('member_id');
        if ($order_id && $member_id){
            $dat=db('order')->where('order_id',$order_id)->find();
            if(!empty($dat)){//mysql
                $da = db('order')->where('order_id',$order_id)->update(
                    [
                        'refund_status'=>1,
                        'updatetime'=>time(),
                    ]
                );
            }else{//mongo
                $KbxOrder = new KbxOrder();
                $dat = $KbxOrder->kbx_order_by_no($order_no);
                //订单系统  更新order数据
                $postUpdateArr = [
                    'id'=>$order_id,
                    'refund'=>array(
                        'refundStatus'=>1
                    ),
                ];
                $update_res = $KbxOrder->kbx_operate_order($dat['orderType'],$postUpdateArr);
                $log[]=[
                    '客户：申请退款'=>json_encode($postUpdateArr),
                    'res'=>$update_res
                ];
                Log::record($log);
                if($update_res){
                    $da = true;
                }else{
                    $da = false;
                }
            }

            $member = db('member')->where('member_id',$member_id)->find();
            if ($da==true){
                $shu=[
                    'code'=>0,
                    'msg'=>'申请成功等待后台处理',
                ];
                $Notify=  new Notify();
                $subject='订单退款申请通知';
                $address='david@kuaibangxing.com';
                $name=$member['member_nickname'];
                $content=$name.'  用户于 '.date('Y-m-d H:i:s',time()).' 时 申请订单退款,订单号<a>'.$order_no.'</a>，请尽快到<a href="https://c.kuaibangxing.com/admin">https://c.kuaibangxing.com</a>进行处理';
                $Notify->kbxemailcode($subject,$content,$address);
                return json_encode($shu);
            }else{
                $shu=[
                    'code'=>101,
                    'msg'=>'参数错误请稍后再试',
                ];
                return json_encode($shu);
            }
        }
    }

    //会员申请
    public function application(){

        $is_apply= input('is_apply');
        $member_id= input('member_id');

        $data= db('member')->where('member_id',$member_id)->update([
            'is_apply'=>$is_apply,
            'updatetime'=>time()
        ]);
        if ($data){
            $shu=[
                'code'=>0,
                'msg'=>'申请成功',
            ];

            return json_encode($shu);
//            $this->success('申请成功','','0');
        }else{
            $shu=[
                'code'=>0,
                'msg'=>'申请失败',
            ];

            return json_encode($shu);
        }
    }

    public function isapply(){//会员中心
        $member_id=input('member_id');
        $data= db('member')->where('member_id',$member_id)->field('is_apply,member_type_id')->find();
        $where['type_status'] = 1;
        $where['member_type_id'] = $data['member_type_id'];
        $member_type_per=db('member_type')->where($where)->field('member_type_id,type_name,description,commission_per_list,discoun_per_list')->find();

        //增加返佣利率数据
        $classify_info = Db::name('classify')->where("classify_status",1)->select();
        $classify_arr = array();
        foreach ($classify_info as $v) {
            $classify_arr[$v['classify_id']] = $v['classify_name'];
        }
        $classify_arr[304] = "接送机";
        $classify_arr[305] = "包车";

        //拆分 折扣率字符串
        $discoun_per_list = $member_type_per['discoun_per_list'];
        if($discoun_per_list && $discoun_per_list!==''){
            $discoun_per_arr = explode(",",$discoun_per_list);
            foreach ($discoun_per_arr as $per_value){
                $per_arr = explode("_",$per_value);
                $per_classify_id = $per_arr[0];
                $per = $per_arr[1];
                $all_per_arr = array();
                if(isset($classify_arr[$per_classify_id])){
                    $all_per_arr['name'] = $classify_arr[$per_classify_id];
                    $all_per_arr['per'] = $per;
                    $member_type_per['discoun_per'][] = $all_per_arr;
                }
            }
        }

        //拆分 返佣利率字符串
        $commission_per_list = $member_type_per['commission_per_list'];
        if($commission_per_list && $commission_per_list!==''){
            $commission_per_arr = explode(",",$commission_per_list);
            foreach ($commission_per_arr as $per_value){
                $per_arr = explode("_",$per_value);
                $per_classify_id = $per_arr[0];
                $per = $per_arr[1];
                $all_per_arr = array();
                if(isset($classify_arr[$per_classify_id])){
                    $all_per_arr['name'] = $classify_arr[$per_classify_id];
                    $all_per_arr['per'] = $per;
                    $member_type_per['commission_per'][] = $all_per_arr;
                }
            }
        }

        $data['member_type']=$member_type_per;
        if ($member_type_per['member_type_id']=='1'&&$data['is_apply']!=1){
            db('member')->where('member_id',$member_id)
                ->update(['is_apply'=>0]);
            $data['is_apply']=0;
        }
        if ($data==true){
            $shu=[
                'code'=>0,
                'msg'=>'ok',
                'data'=>$data
            ];
            return json_encode($shu);
        }else{
            $shu=[
                'code'=>0,
                'msg'=>'请求失败',
            ];
            return json_encode($shu);
        }
    }

    //通过评论id获取评论和商品详情
    public function getevaluateList(){
        $id=$this->request->param('comment_id');
        if(empty($id)){
            $arr=['code'=>1,'mg'=>'缺失参数comment_id','data'=>''];
            return json_encode($arr);
        }
        //获取产品的页面和标题、地点、销量
        $data=Db::name('comment')->where(['comment_id'=>$id])->field('goods_id,content,comment_img')->find();
        if(empty($data)){
            $arr=['code'=>1,'mg'=>'未查询到相关信息','data'=>''];
            return json_encode($arr);
        }
        $goods=Db::name('goods')
            ->alias('a')
            ->join('city c','a.city_id=c.id','left')
            ->where(['goods_id'=>$data['goods_id']])->field('a.goods_name,a.goodssales,a.invented_sold,a.goods_image,c.name as city')->find();

        //图片仅返回一条
        if(!empty($goods['goods_image'])){
            $imges=explode(',',$goods['goods_image']);
            $imges=array_filter($imges);
            if(count($imges) > 0){
                $goods['goods_image'] = array_shift($imges);
            }
        }
        if(!empty($goods['invented_sold'])){
            $goods['goodssales'] = $goods['goodssales'] + $goods['invented_sold'];
        }
        //获取评论
        $arr=['code'=>0,'mg'=>'获取信息成功','data'=>['comment'=>$data,'goods'=>$goods]];
        return json_encode($arr);
    }


}